<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd ? '添加管理员' : '编辑管理员'"
      :visible.sync="dialogInfo.isShow"
    >
      <el-form :rules="rules" :model="userform" ref="userInfo">
        <el-form-item
          label="所属角色："
          :label-width="formLabelWidth"
          prop="roleid"
        >
          <el-select v-model="userform.roleid"  placeholder="请选择">
            <el-option  v-for="item in roleList"
              :key="item.id"
              :label="item.rolename"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="角色名称："
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="userform.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码：" :label-width="formLabelWidth">
          <el-input v-model="userform.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状 态：" :label-width="formLabelWidth">
          <el-switch
            v-model="userform.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { postAddUser, getUserInfo, postEditUser } from "../../util/axios";
import { mapGetters, mapActions } from "vuex";
export default {
  data() {
    return {
      formLabelWidth: "100px",
      userform: {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      },
      rules: {
        roleid: [
          { required: true, message: "请输入用户名称", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请请选择所属角色", trigger: "blur" },
          { min: 2, max: 15, message: "长度在 2 到 15 个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["dialogInfo"],
  computed: {
    ...mapGetters({
      roleList: "role/getRoleList",
    }),
  },
  mounted() {
    this.getRoleAction();
  },
  methods: {
    ...mapActions({
      getRoleAction: "role/getRoleListAction",
      getUserListAction: "manger/getUserListAction",
      getCountAction:'manger/getUserCountAction'
    }),
    reset() {
      this.userform = {
        roleid: "",
        username: "",
        password: "",
        status: 1,
      };
      this.$refs.userInfo.resetFields();
    },
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    lookup(uid) {
      getUserInfo({ uid }).then((res) => {
        this.userform = res.data.list;
        this.userform.uid = uid;
      });
    },
    confirm() {
      this.$refs.userInfo.validate((valid) => {
        if (valid) {
          // this.roleform.menus = this.$refs.tree.getCheckedKeys().join(",");
          if (this.dialogInfo.isAdd) {
            postAddUser(this.userform).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.cancel();
                this.getUserListAction();
                this.getCountAction()
              } else {
                this.$message.error(res.data.msg);
              }
            });
          } else {
            // hahahah
            postEditUser(this.userform).then((res) => {
              if (res.data.code == 200) {
                this.$message.success(res.data.msg);
                this.cancel();
                this.getUserListAction();
                this.getCountAction()
              } else {
                this.$message.error(res.data.msg);
              }
            });
          }
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
      this.cancel()
    },
  },
};
</script>

<style lang="" scoped>
</style>
